package com.allwees.bs.core.model.product.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.allwees.bs.core.model.util.PhotosStr;
import com.allwees.bs.core.model.util.PriceShouldAdjusted;
import com.allwees.bs.core.model.util.TotalSalesRandom;
import com.allwees.bs.core.modelbase.entity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper=true)
@ToString(callSuper = true)
@NoArgsConstructor
@MappedSuperclass
public class ProductModel extends BaseEntity implements PhotosStr, PriceShouldAdjusted {

	private static final long serialVersionUID = -8352319124732295695L;

	/**
	 * 供应商的商品(手动新增/批量导入)默认的score
	 */
	public static final double DEFAULT_SCORE = 10000D;
	/**
	 * 供应商uuid
	 */
    @Column(updatable = false, length = 48)
    private String supplierUuid;

    /**
     * 零售商uuid
     */
    @Column(updatable = false, length = 48)
    private String retailerUuid;

    /**
     * 店铺uuid
     */
    @Column(updatable = false, length = 48)
    private String storeUuid;

	/**
	 * 商品编号（标准规范格式）
	 */
	@Column(unique = true, length = 64)
	protected String no;

	/**
	 * 定义的编号
	 */
	@Column(unique = true, length = 64)
	protected String defNo;

	/**
	 * 商品编号（自定义）
	 */
	@Column(length = 32)
	protected String customNo;

	/**
	 * 类别编号
	 */
	@Column(length = 12)
	protected String categoryNo;

	@TableField(exist=false)
	@Transient
	private String categoryFullname;

	/**
	 * 商品名称
	 */
	@Column(length = 512)
	protected String name;

	/**
	 * 关键字，逗号分隔
	 */
	@Column(length = 512)
	protected String keywords;

	/**
	 * 商品描述
	 */
	@Column(length = 40960)
	protected String description;

	/**
	 * 主图地址，URI
	 */
	@Column(length = 512)
	protected String mainPhoto;

	/**
	 * 图片地址，多个图片用','拼接
	 */
	@JsonIgnore
    @JSONField(serialize = false, deserialize = false)
	@Column(length = 8192)
	private String photosStr;

	/**
	 * 零售原价
	 */
    @Column(precision = 8, scale = 2)
	protected BigDecimal originalPrice;

	/**
	 * 零售现价
	 */
    @Column(precision = 8, scale = 2)
	protected BigDecimal retailPrice;

	protected BigDecimal shippingPrice;

	/**
	 * 零售价币种
	 */
	@Column(length = 4)
	protected String currency;

	/**
	 * 快递预计到达时间描述
	 */
	protected String shippingArrivalDesc;

	/**
	 * 最小运输天数
	 */
	@Column(columnDefinition = "INT1 UNSIGNED")
	protected Integer shippingMinDays;

	/**
	 * 最大运输天数
	 */
	@Column(columnDefinition = "INT1 UNSIGNED")
	protected Integer shippingMaxDays;

	/**
	 * 商家未发货时的自动退款所需要等待的天数
	 */
	@Column(columnDefinition = "INT1 UNSIGNED")
	protected Integer autoRefundDays;

	/**
	 * 用户评分
	 */
	protected Float rating;

	/**
	 * 算法得分
	 */
	protected Double score;

	/**
	 * 总销量数
	 */
	protected Integer totalSales;
	/**
	 * 总销量数-虚拟
	 */
	protected Integer salesTotal;
	/**
	 * 总退货数
	 */
	protected Integer totalRefunds;

	/**
	 * 总收藏数
	 */
	protected Integer totalCollections;

	/**
	 * 总评论数
	 */
	protected Integer totalReviews;

	/**
	 * 总分享数
	 */
	protected Integer totalShares;

	/**
	 * 总浏览数
	 */
	protected Integer totalViews;

	/**
	 * 发布日期
	 */
	protected Date publishAt;

	/**
	 * 退货率
	 */
	protected Float refundRate;

	@JsonIgnore
    @JSONField(serialize = false)
	public String getValidNo() {
		return StringUtils.isNotBlank(customNo) ? customNo : no;
	}

	/**
	 * 调价后的商品原件
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	protected BigDecimal priceOrigin;

	/**
	 * 调价后的商品售价
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	protected BigDecimal priceRetail;

	/**
	 * 调价后的商品运费
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	protected BigDecimal priceShip;

	public static final int MARKETING_TYPE_0= 0;
	/**
	 * 网红
	 */
	public static final int MARKETING_TYPE_1 = 1;
	/**
	 * 新手
	 */
	public static final int MARKETING_TYPE_2 = 1 << 1;
	/**
	 * 闪购
	 */
	public static final int MARKETING_TYPE_4 = 1 << 2;
	/**
	 * 热销产品
	 */
	public static final int MARKETING_TYPE_8 = 1 << 3;

	/**
	 * 商品营销类型
	 */
	protected Integer marketingType;

	/**
	 * 优惠力度 闪购商品特有 e.g.:-80%
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String discountOff;

	/**
	 * 免邮标签
	 */
	@Transient
	@TableField(exist=false)
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String freePost;

	/**
	 * 每个用户限购数量
	 */
	private Integer numPerUser;
//	/**
//	 * almost gone 标签
//	 */
//	@Transient
//	@TableField(exist=false)
//	@JsonInclude(JsonInclude.Include.NON_NULL)
//	private String almostGone;

	@JsonIgnore
	public boolean isFlashProduct(){
		return (marketingType != null) && ((marketingType & MARKETING_TYPE_4) == MARKETING_TYPE_4);
	}

	@JsonIgnore
	public boolean isFreeGift(){
		return (marketingType != null) && ((marketingType & MARKETING_TYPE_2) == MARKETING_TYPE_2);
	}

	@JsonIgnore
	public void attachRandomSales(){
		this.salesTotal = TotalSalesRandom.next();
	}
}
